#include "helper.h"
#include <QSqlField>
#include <QSqlDriver>

Helper::Helper(QObject *parent)
	: QObject(parent)
{

}

Helper::~Helper()
{

}

DBAdapter * Helper::dbadapter = NULL;

void Helper::exportLessonData(int lessonid)
{
	SqlQuery sq;
	//sq.exec(QString("").arg(lessonid));

	//clear import/export tables
	sq.exec(QString("delete from lesson_import"));
	sq.exec(QString("delete from lesson_args"));
	sq.exec(QString("delete from varmodcomments_import"));
	sq.exec(QString("delete from choosenvars_import"));

	//fill lesson args
	sq.exec(QString("INSERT INTO lesson_args (teacherid, placeid, lessontypeid, groupid, dateoflesson) SELECT teacherid, placeid, lessontypeid, groupid, dateoflesson FROM lessons WHERE id=%1").arg(lessonid));

	//quiz attempts
	sq.exec(QString("INSERT INTO lesson_import (studentid, lessontypeid, typeofrow, quizgrade, timeevent) SELECT studentid, lessontypeid, 1, grade, timeattempt FROM quiz_attempts WHERE lessonid=%1").arg(lessonid));
	//srs tesing
	sq.exec(QString("INSERT INTO lesson_import (studentid, lessontypeid, typeofrow, variantid, formallycorrect, alltestspassed, comments, timeevent)SELECT studentid, lessontypeid, 2, variantid, formallycorrect, alltestspassed, srs_attempts.comments, timetesting FROM srs_attempts JOIN variants ON variantid=variants.id WHERE lessonid=%1").arg(lessonid));
	//mod selected
	sq.exec(QString("INSERT INTO lesson_import (studentid, lessontypeid, typeofrow, variantid, modid, timeevent) SELECT studentid, lessontypeid, 3, variantid, modid, timegiven FROM mod_attempts JOIN modifications ON modid=modifications.id JOIN variants ON variantid=variants.id WHERE lessonid=%1").arg(lessonid));
	//mod testing
	sq.exec(QString("INSERT INTO lesson_import (studentid, lessontypeid, typeofrow, variantid, modid, timeevent, formallycorrect, alltestspassed, comments) SELECT studentid, lessontypeid, 4, variantid, modid, timetesting, formallycorrect, alltestspassed, mod_testing.comments FROM mod_testing JOIN mod_attempts ON modattemptid=mod_attempts.id JOIN modifications ON modid=modifications.id JOIN variants ON variantid=variants.id WHERE lessonid=%1").arg(lessonid));

	//export var and mod comments
	sq.exec(QString("INSERT INTO varmodcomments_import (variantid, modid, comments) SELECT id, -1, comments FROM variants WHERE comments IS NOT NULL AND comments <> ''"));
	sq.exec(QString("INSERT INTO varmodcomments_import (variantid, modid, comments) SELECT variantid, id, comments FROM modifications WHERE comments IS NOT NULL AND comments <> ''"));

	//export choosen variants
	sq.exec(QString("INSERT INTO choosenvars_import (studentid, variantid, active) SELECT studentid, variantid, active FROM choosen_variants WHERE lessonid=%1").arg(lessonid));
}

void Helper::exportUnlessonData(bool markAsExported)
{
	SqlQuery sq;

	//clear import/export table
	sq.exec(QString("delete from unlesson_import"));

	//code style attempts
	sq.exec(QString("INSERT INTO unlesson_import (lessontypeid, studentid, teacherid, typeofrow, minimum, isinmoodle, grade, comments, timegraded) SELECT lessontypeid, studentid, teacherid, 1, minimum, isinmoodle, grade, comments, timegraded FROM codestyle_attempts WHERE exported=0"));
	//protocol attempts
	sq.exec(QString("INSERT INTO unlesson_import (lessontypeid, studentid, teacherid, typeofrow, minimum, grade, comments, timegraded) SELECT lessontypeid, studentid, teacherid, 2, minimum, grade, comments, timegraded FROM protocol_attempts WHERE exported=0"));
	//algorithm attempts
	sq.exec(QString("INSERT INTO unlesson_import (lessontypeid, studentid, teacherid, typeofrow, grade, comments, timegraded) SELECT lessontypeid, studentid, teacherid, 3, grade, comments, timegraded FROM algorithm_attempts WHERE exported=0"));

	if (markAsExported) {//set records as exported
		sq.exec(QString("UPDATE codestyle_attempts SET exported=1 WHERE exported=0"));
		sq.exec(QString("UPDATE protocol_attempts SET exported=1 WHERE exported=0"));
		sq.exec(QString("UPDATE algorithm_attempts SET exported=1 WHERE exported=0"));
	}
}

QModelIndex Helper::findInModelColumn(QAbstractItemModel * model, int value, int column)
{
	QModelIndex indx;
	bool find = false;
	for (int row=0; row < model->rowCount() && !find; row++){
		indx = model->index(row, column);
		if (model->data(indx).toInt() == value)
			find = true;
	}
	
	return indx;
}

int Helper::getIntFromModel(QAbstractItemModel * model, int row, int column)
{
	if (column < 0) {//column not specified, use last column
		column = model->columnCount() - 1;
	}
	QModelIndex index = model->index(row, column);
	return model->data(index).toInt();
}

QString Helper::pathToTests(int lessontypenum, int variantnum, int modnum)
{
	return  QString("./autotester/Lab_%1/%2/%3/").arg(lessontypenum).arg(variantnum, 2, 10, QChar('0')).arg(modnum);
}

void Helper::formatStringToDb(QString &str)
{
	QSqlDatabase db = QSqlDatabase::database();
	QSqlField fld = QSqlField(QString(""),QVariant::String);
	fld.setValue(str);
	str = db.driver()->formatValue(fld);
}